package com.vsd.mobile;



import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cyz.domain.CateRecommend;
import com.demo.annotation.MobileMethod;
import com.demo.mobile.MobException;
import com.demo.mobile.MobileInfo;
import com.demo.util.MathUtil;
import com.demo.util.ProperityUtil;
import com.shopcart.domain.StoreCategory;
import com.vsd.daoEx.model.IndexActStoreEx;
import com.vsd.domain.IndexActivity;
import com.vsd.repository.CateRecommendRepository;
import com.vsd.repository.IndexActivityRepository;
import com.vsd.repository.StoreCategoryRepository;
import com.vsd.service.IndexActGoodsService;
import com.vsd.service.IndexActStoreService;
import com.vsd.service.StoreGoodsService;
import com.vsd.service.StoreService;
import com.zy.index.proto.MOIndex.MActivity;
import com.zy.index.proto.MOIndex.MActivityList;
import com.zy.index.proto.MOIndex.MCategoryList;
import com.zy.index.proto.MOIndex.MStoreCategory;
import com.zy.store.proto.MOAppStore.MStoreMini;
import com.zy.store.proto.MOAppStore.MStoreMiniList;
import com.zy.store.proto.MOAppStore.MTgGoodsMiniList;

@Controller
@RequestMapping("/mobile/")
public class MIndexController {
	
	 @Autowired
	 CateRecommendRepository cateRecommendRepository;
	 @Autowired
	 StoreCategoryRepository storeCategoryRepository;
	 @Autowired
	 IndexActivityRepository indexActivityRepository;
	 @Autowired
	 StoreService storeService;
	 @Autowired
	 IndexActStoreService indexActStoreService;
	 @Autowired
	 IndexActGoodsService indexActGoodsService;
	 @Autowired
	 StoreGoodsService storeGoodsService;
	 @Autowired
	 ProperityUtil properityUtil;
	 
	/**
	@api mCategoryList mCategoryList	//商家分类列表
	@param optional number location  //location 1首页推荐分类  2普通商家分类
	@return MCategoryList
	*/
	@RequestMapping("/mCategoryList")
	@MobileMethod
	public MCategoryList.Builder mCategoryList(MobileInfo mobileInfo,Integer location) throws Exception {
		MCategoryList.Builder ret = MCategoryList.newBuilder();
		if(location==null) location=1;
		if(location==1){
			Page<CateRecommend> recommend = cateRecommendRepository.findByAreaCode(mobileInfo.getAreaCode(),new PageRequest(0, 7,new Sort(Direction.DESC, "sort")));
			for (CateRecommend r : recommend) {
				MStoreCategory.Builder mini = MStoreCategory.newBuilder();
				mini.setIcon(r.getImg());
				mini.setId(r.getCateCode());
				mini.setTitle(r.getName());
				ret.addList(mini);
			}
		}else if(location==2){
			Collection<StoreCategory> list = storeCategoryRepository.findByLevel(1,new Sort(Direction.DESC, "sort"));
			MStoreCategory.Builder m = MStoreCategory.newBuilder();
			m.setIcon(properityUtil.getSelf_url()+"h5/images/ic_cate_all.png");
			m.setId("0");
			m.setTitle("全部");
			ret.addList(m);
			for (StoreCategory r : list) {
				MStoreCategory.Builder mini = MStoreCategory.newBuilder();
				mini.setIcon(r.getLogo());
				mini.setId(r.getCode());
				mini.setTitle(r.getName());
				ret.addList(mini);
			}
		}
		
		return ret;
	}
	 
	/**
	@api mActivityList mActivityList	//活动列表
	@param optional number location  //location 1首页活动
	@return MActivityList
	*/
	@RequestMapping("/mActivityList")
	@MobileMethod
	public MActivityList.Builder mActivityList(MobileInfo mobileInfo,Integer location) throws Exception {
		if(location==null) location = 1;
		MActivityList.Builder ret = MActivityList.newBuilder();
		
		if(location==1){//首页活动
			Page<IndexActivity> list = indexActivityRepository.findByAreaCodeAndState(mobileInfo.getAreaCode(),1,new PageRequest(0, 5,new Sort(Direction.DESC, "sortOrder")));
			for (IndexActivity i : list) {
				MActivity.Builder act = MActivity.newBuilder();
				act.setIcon(i.getImg());
				act.setId(i.getId());
				act.setTitle(i.getTitle());
				act.setType(i.getType());
				ret.addList(act);
			}
		}
		return ret;
	}
	 
	/**
	@api mActivityStoreList mActivityStoreList	//活动商家列表
	@param optional string id  //活动id
	@return MStoreMiniList
	*/
	@RequestMapping("/mActivityStoreList")
	@MobileMethod(isPage=true)
	public MStoreMiniList.Builder mActivityStoreList(MobileInfo mobileInfo,String id,String lng,String lat,Integer page,Integer limit) throws Exception {
		if(StringUtils.isBlank(id)) throw new MobException("id");
		
		MStoreMiniList.Builder ret = MStoreMiniList.newBuilder();
		Collection<IndexActStoreEx> list = indexActStoreService.selectAppList(id, lng, lat,page,limit);
		
		for (IndexActStoreEx i : list) {
			MStoreMini.Builder mini = MStoreMini.newBuilder();
			mini.setCateName(i.getCateName());
			mini.setCityName(i.getAreaName());
			mini.setCommentCnt(MathUtil.formatAsComment(i.getCommentCnt()));
			mini.setDistance(MathUtil.getDistanceStr(i.getDistance()));
			mini.setId(i.getStoreId());
			mini.setLogo(i.getLogo());
			mini.setStar(new Double(i.getStar()));
			mini.setTitle(i.getStoreName());
			ret.addList(mini);
 		}
		return ret;
	}

	/**
	@api mActivityGoodsList mActivityGoodsList	//活动团购列表
	@param optional string id  //活动id
	@return MTgGoodsMiniList
	*/
	@RequestMapping("/mActivityGoodsList")
	@MobileMethod(isPage=true)
	public MTgGoodsMiniList.Builder mActivityGoodsList(MobileInfo mobileInfo,String lat,String lng,String id,Integer page,Integer limit) throws Exception {
		if(StringUtils.isBlank(id)) throw new MobException("id");
		return indexActGoodsService.selectAppList(id, lat, lng, page, limit);
	}


	/**
	@api mGuess mGuess	//首页猜你喜欢(团购)
	@return MTgGoodsMiniList
	*/
	@RequestMapping("/mGuess")
	@MobileMethod(isPage=true)
	public MTgGoodsMiniList.Builder mGuess(MobileInfo mobileInfo,String lat,String lng) throws Exception {
		return storeGoodsService.findGuess(lat, lng);
	}

	

 
	 
}
